home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: 12 bit datatype in binary file - misaligned
- Date: 22 Mar 1996 09:15:18 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4iun76$bh2@crl.crl.com>
- References: <4itqv9$kcq@news.ccit.arizona.edu>
- NNTP-Posting-Host: crl.com
-
- chrisl@SEDS.LPL.Arizona.EDU (Chris Lewicki) writes:
-
- > I've been scouring the net and the bookstores for the last few
- >days trying to figure out how to read a 12-bit data length out of a
- >binary file.
- > Here's the setup: I have a data file that has all lengths of
- >fields in it. There are 1, 2, 3, 4, 6, 8, 12, and 16 bit data types
- >all packed in very efficiently. I've figured out that you can define
- >a structure:
-
- >struct BitData {
- > unsigned onebit : 1;
- > unsigned anotheronebit : 1;
- > unsigned fourbit : 4;
- > unsigned twobit : 2;
- > unsigned : 4; /* 4 bits padding */
- > unsigned char eightbit;
- > unsigned twelvebit : 12;
- > unsigned sixteenbit;
- >};
-
- Sorry, but it generally won't be possible to do a simple read of data
- like this. On a system where bytes are 8 bits, the system will always
- insert an extra four bits padding before the 'eightbit' field. (Well... C
- doesn't /require/ it to do so, but I've never seen it done otherwise. It
- doesn't prevent it, either...).
-
- I'm assuming you want to read in an 8-byte value for bitdata (48 bits)?
- In that case, your best bet is to read in an 8-byte string, and pull the
- bits out yourself. It's even (mostly) portable.
-
- Sorry that's not what you wanted to hear. Good luck.
-
-
-